package com.csw.android.dev_utils.ui.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.csw.android.dev_utils.R
import com.csw.android.dev_utils.databinding.DevUtilsDialogCommonBinding
import com.csw.android.dev_utils.ex.ContextEx.getSystemDrawable
import com.csw.android.dev_utils.ex.ContextEx.getThemeColor
import com.csw.android.dev_utils.ex.ViewEx.setOnClickListener
import com.csw.android.dev_utils.ui.IUICreator

abstract class CommonDialog<V : ViewDataBinding>(context: Context) : LifecycleDialog(context),
    IUICreator {
    private val dataBinding: DevUtilsDialogCommonBinding
    val contentBinding: V

    var cancelEnable = true

    init {
        dataBinding =
            DataBindingUtil.inflate(layoutInflater, R.layout.dev_utils_dialog_common, null, false)
        contentBinding = DataBindingUtil.inflate(
            layoutInflater,
            getContentViewID(),
            dataBinding.contentContainer,
            true
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(dataBinding.root)
        window?.run {
            decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            //设置状态栏颜色
            statusBarColor = context.getThemeColor(android.R.attr.colorPrimary)

            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            val lp = attributes
            lp.width = WindowManager.LayoutParams.MATCH_PARENT
            lp.height = WindowManager.LayoutParams.MATCH_PARENT
            lp.dimAmount = 0.0f  //修改背景半透明程度
            attributes = lp
        }
        initView(dataBinding.root, null)
        initAdapter()
        initListener()
        initData()
    }

    override fun setTitle(title: CharSequence?) {
        super.setTitle(title)
        dataBinding.title.text = title
    }

    override fun initView(rootView: View, savedInstanceState: Bundle?) {

    }

    override fun initAdapter() {
    }

    override fun initListener() {
        dataBinding.cardView.setOnClickListener {}
        dataBinding.root.setOnClickListener {}
        dataBinding.run {
            arrayListOf(rootView, cardView).setOnClickListener {
                when (it.id) {
                    R.id.rootView -> {
                        if (cancelEnable) {
                            cancel()
                        }
                    }
                }
            }
        }
    }

    override fun initData() {
    }

    override fun setCancelable(flag: Boolean) {
        super.setCancelable(flag)
        cancelEnable = flag
    }

    /**
     * 设置水平边距
     */
    fun setHorizontalPadding(px: Int) {
        dataBinding.cardView.run {
            layoutParams.run {
                if (this is ViewGroup.MarginLayoutParams) {
                    marginStart = px
                    marginEnd = px
                }
            }
            requestLayout()
        }
    }

    /**
     * 添加底部菜单
     */
    fun addBottomButton(@StringRes resId: Int, listener: View.OnClickListener): TextView {
        return TextView(context).apply {
            background = context.getSystemDrawable(android.R.attr.selectableItemBackground)
            setTextColor(context.getThemeColor(com.google.android.material.R.attr.colorOnPrimary))
            setText(resId)
            setOnClickListener(listener)
            setTypeface(typeface, Typeface.BOLD)
            gravity = Gravity.CENTER
            dataBinding.bottomButtons.addView(
                this,
                LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT).apply {
                    weight = 1f
                }
            )
        }
    }

    /**
     * 移除菜单
     */
    fun removeBottomButton(view: View) {
        dataBinding.bottomButtons.removeView(view)
    }
}